﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheckTTS
{
    public partial class MainForm : Form
    {
        Stack<TagInfo> mStackTag = new Stack<TagInfo>();
        List<ErrorInfo> mErrorList = new List<ErrorInfo>();
       // OpenFileDialog mOFD = new OpenFileDialog();
        CheckHandle mSpanTagCheckHandler = null;
        CheckHandle mSpanAttrsCheckHandle = null;
        CheckHandle mSpanContentCheckHandle = null;

        private enum EnumMousePointPosition
        {
            MouseSizeNone = 0, //'无
        //    MouseSizeRight = 1, //'拉伸右边框
           // MouseSizeLeft = 2, //'拉伸左边框
            MouseSizeBottom = 3, //'拉伸下边框
            MouseSizeTop = 4, //'拉伸上边框
           // MouseSizeTopLeft = 5, //'拉伸左上角
          //  MouseSizeTopRight = 6, //'拉伸右上角
           // MouseSizeBottomLeft = 7, //'拉伸左下角
           // MouseSizeBottomRight = 8, //'拉伸右下角
          //  MouseDrag = 9 // '鼠标拖动
        }

        const int Band = 5;
        const int MinWidth = 10;
        const int MinHeight = 10;
        private EnumMousePointPosition m_MousePointPosition;
        private Point mBegPoint, mCurrentPoint;

        public MainForm()
        {
            InitializeComponent();
            check_content_richTextBox.LanguageOption = RichTextBoxLanguageOptions.UIFonts;

            this.KeyDown += OnKeyDown;
            foreach (Control control in this.Controls)
            {
                control.KeyDown += OnKeyDown;
            }
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                if (e.KeyValue == (int)Keys.S)
                {
                    if (string.IsNullOrEmpty(check_file_path_textBox.Text)) {
                        return;
                    }

                    saveFile(check_file_path_textBox.Text);
                    this.check_content_richTextBox.LoadFile(check_file_path_textBox.Text, RichTextBoxStreamType.PlainText);
                    checkContent();

                    save_check_file_button.Enabled = false;
                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // this.check_content_richTextBox.LoadFile(@"C:\Users\youjie\Desktop\tts标签校验需求\人间喜剧2.txt", RichTextBoxStreamType.PlainText);
            // checkSpanTag();
            Text = System.Configuration.ConfigurationSettings.AppSettings["version"].ToString(); 
            initCheckHandler();
            save_check_file_button.Enabled = false;
            label_error_count.Text = "发现:0个错误";
            
        }

        private void initCheckHandler() {
            try
            {
                mSpanTagCheckHandler = new SpanTagCheckHandle(this);
                mSpanAttrsCheckHandle = new SpanAttrsCheckHandle(this);
                mSpanContentCheckHandle = new SpanContentCheckHandle();
                mSpanTagCheckHandler.setSuccessor(mSpanAttrsCheckHandle);
                mSpanAttrsCheckHandle.setSuccessor(mSpanContentCheckHandle);
            }
            catch (Exception e) {
                Console.WriteLine(e.StackTrace);
            }
        }

        private void checkContent()
        {
            if (mErrorList != null) {
                mErrorList.Clear();
            }

            if(mStackTag != null){
                mStackTag.Clear();
            }
            
            error_output_richTextBox.Text = "";
            if (mSpanTagCheckHandler != null) {
                mSpanTagCheckHandler.checkHandle(check_content_richTextBox.Text, mErrorList, mStackTag, check_content_richTextBox);
            }
        }

        private void import_check_file_button_Click(object sender, EventArgs e)
        {
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.Title = "打开文件";
            oFD.ShowHelp = true;
            oFD.Filter = "文本文件|*.txt";

            oFD.FilterIndex = 1;
            oFD.RestoreDirectory = false;
            oFD.InitialDirectory = "c:\\";
            oFD.Multiselect = false;
            if (oFD.ShowDialog() == DialogResult.OK)
            {
                this.check_file_path_textBox.Text = oFD.FileName;
                this.check_content_richTextBox.LoadFile(oFD.FileName, RichTextBoxStreamType.PlainText);
                checkContent();
            }
            save_check_file_button.Enabled = false;
        }

        private void save_check_file_button_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(check_file_path_textBox.Text))
            {
                return;
            }

            saveFile(check_file_path_textBox.Text);
            this.check_content_richTextBox.LoadFile(check_file_path_textBox.Text, RichTextBoxStreamType.PlainText);
            checkContent();
            save_check_file_button.Enabled = false;
        }

        private void save_as_check_file_button_Click(object sender, EventArgs e)
        {
            SaveFileDialog sFileDialog = new SaveFileDialog();
            sFileDialog.Filter = "文本文件|*.txt";

            //设置默认文件类型显示顺序 
            sFileDialog.FilterIndex = 1;
            sFileDialog.RestoreDirectory = true;

            if (sFileDialog.ShowDialog() == DialogResult.OK)
            {

                string localFilePath = sFileDialog.FileName.ToString(); //获得文件路径                 
                saveFile(localFilePath);

                this.check_content_richTextBox.LoadFile(localFilePath, RichTextBoxStreamType.PlainText);
                checkContent();
                if (localFilePath.Equals(check_file_path_textBox.Text))
                {
                    save_check_file_button.Enabled = false;
                }
                this.check_file_path_textBox.Text = localFilePath;
            }
        }


        private void saveFile(string fileName) {
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            try
            {
                this.check_content_richTextBox.SaveFile(fileName, RichTextBoxStreamType.PlainText);
              //  MessageBox.Show("保存成功");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("保存失败");
            }
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            mBegPoint.X = e.X;
            mBegPoint.Y = e.Y;

            mCurrentPoint.X = e.X;
            mCurrentPoint.Y = e.Y;
           // Console.WriteLine("mouseDown p.y:"+e.Y);
        }

        private void MainForm_MouseLeave(object sender, EventArgs e)
        {
            m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
            this.Cursor = Cursors.Arrow;
        }

        private EnumMousePointPosition mousePointPosition(Control control, System.Windows.Forms.MouseEventArgs e)
        {
            Size size = control.Size;
           // Console.WriteLine("e.y:"+e.Y+" control:height"+Size.Height+" controlName:"+control.Name);
           
            if (size.Height<Band) {
                return EnumMousePointPosition.MouseSizeNone;
            }

            if (control == check_content_richTextBox) {
                if(e.Y < Band) {
                    return EnumMousePointPosition.MouseSizeNone;
                }

                if (e.Y>size.Height -15) {
                   // Console.WriteLine("mouse size Bottom");
                    return EnumMousePointPosition.MouseSizeBottom;
                }
            }
            //else {
            //    if (e.Y < Band)  {
            //        Console.WriteLine("mouse size Top");
            //        return EnumMousePointPosition.MouseSizeTop;
            //    }

            //    if (e.Y > size.Height - Band)  {
            //        return EnumMousePointPosition.MouseSizeNone;
            //    }
            //}
            return EnumMousePointPosition.MouseSizeNone;
        }

        private void check_content_richTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.check_file_path_textBox.Text)) {
                this.save_check_file_button.Enabled = true;
            } else {
                this.save_check_file_button.Enabled = false;
            }
        }

        private void error_output_richTextBox_DoubleClick(object sender, EventArgs e)
        {
            int cursorPosition = error_output_richTextBox.SelectionStart;
            int lineIndex = error_output_richTextBox.GetLineFromCharIndex(cursorPosition);
            string lineText = error_output_richTextBox.Lines[lineIndex];
            Console.WriteLine(lineText);

            ErrorInfo errorInfo = mErrorList.ElementAt(lineIndex);
            Console.WriteLine(lineText);
            check_content_richTextBox.SelectionStart = errorInfo.index;
            check_content_richTextBox.Focus();
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e) {
           Control lCtrl =(sender as Control);
          
           if (e.Button==MouseButtons.Left)
           {
               // Console.WriteLine(lCtrl.Name);
            switch (m_MousePointPosition)
            {
             case EnumMousePointPosition.MouseSizeBottom:
                if (this.check_content_richTextBox.Height + e.Y - mCurrentPoint.Y >MinHeight &&
                    this.error_output_richTextBox.Height + mCurrentPoint.Y - e.Y>MinHeight) {
                    this.check_content_richTextBox.Height = this.check_content_richTextBox.Height + e.Y - mCurrentPoint.Y;
                    this.error_output_richTextBox.Top = this.error_output_richTextBox.Top - mCurrentPoint.Y + e.Y;
                            
                    this.error_output_richTextBox.Height = this.error_output_richTextBox.Height + mCurrentPoint.Y - e.Y;
                   // this.label_error_count.Top = this.label_error_count.Top + -mCurrentPoint.Y + e.Y;
                    //this.label_info_display.Top = this.label_info_display.Top + -mCurrentPoint.Y + e.Y;
                    this.panel1.Top = this.panel1.Top + -mCurrentPoint.Y + e.Y;
                            check_content_richTextBox.ScrollToCaret();
                }
                mCurrentPoint.Y=e.Y; //'记录光标拖动的当前点
               // Console.WriteLine(lCtrl.Name + " mouseSizeBottom");
                break;
           //  case EnumMousePointPosition.MouseSizeTop:
                  //Console.WriteLine(lCtrl.Name + " MouseSizeTop e.Y:"+e.Y+" currentY:"+mCurrentPoint.Y+" (e.y-curr.Y):"+(e.Y - mCurrentPoint.Y));
                 // this.check_content_richTextBox.Height = this.check_content_richTextBox.Height + (e.Y - mCurrentPoint.Y);
                       // Console.WriteLine("control:height" + this.check_content_richTextBox.Height);
                        //this.error_output_richTextBox.Top = this.error_output_richTextBox.Top +(e.Y - mCurrentPoint.Y);
                        //this.error_output_richTextBox.Height = this.error_output_richTextBox.Height +(e.Y - mCurrentPoint.Y );
                 //       mCurrentPoint.Y = e.Y;
              //  break;
              default:
                break;
            }
                // if (lCtrl.Width<MinWidth)
                //     lCtrl.Width=MinWidth;
                if (check_content_richTextBox.Height < MinHeight) {
                    check_content_richTextBox.Height = MinHeight;
                }

                if (error_output_richTextBox.Height < MinHeight) {
                    error_output_richTextBox.Height = MinHeight;
                }
            }
           else {
            m_MousePointPosition = mousePointPosition(lCtrl, e); //'判断光标的位置状态
            switch (m_MousePointPosition) //'改变光标
            {
              case EnumMousePointPosition.MouseSizeNone:
                this.Cursor = Cursors.Arrow;       //'箭头
                break;
              case EnumMousePointPosition.MouseSizeBottom:
                this.Cursor = Cursors.SizeNS;      //'南北
                break;
              case EnumMousePointPosition.MouseSizeTop:
                this.Cursor = Cursors.SizeNS;      //'南北
                break;

              default:
                break;
             }
          }
         }//end of MainForm_MouseMove
    
    }
}
